package com.hz.controller;

import com.aliyun.oss.common.utils.DateUtil;
import com.hz.dao.WashHouseDao;
import com.hz.pojo.PicUploadResult;
import com.hz.pojo.WashHouse;
import com.hz.pojo.WashMachine;
import com.hz.service.WashMachineService;
import com.hz.service.impl.PicUploadServiceImpl;
import com.hz.utils.DateUtils;
import com.hz.utils.MassageJson;
import com.hz.utils.MassageJsons;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/WashMachine")
public class WashMachineController {
    @Resource
    PicUploadServiceImpl picUploadService;
    @Resource
    private WashMachineService washMachineService;
    @Resource
    WashHouseDao washHouseDao;
    @RequestMapping("/getFunction")
    public MassageJson getFunction(Long wash_machine_id){
        WashMachine washMachine = null;
        try {
            washMachine = washMachineService.getFunction(wash_machine_id);
            if(washMachine!=null){
                return new MassageJson(200,"请求成功",washMachine);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new MassageJson(201,"请求失败",washMachine);
    }

    @RequestMapping("/getWashMachineList")
    public MassageJson getWashMachineList(Long washhouse_id){
        List<WashMachine> washMachineList = null;
        try {
            washMachineList = washMachineService.getWashMachineList(washhouse_id);
            if(washMachineList.size()>0){
                return new MassageJson(200,"获取成功",washMachineList);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new MassageJson(201,"获取失败",washMachineList);
    }

    /**
     * 添加洗衣机
     */
    @RequestMapping("/addWashMachine")
    @Transactional
    public MassageJsons addWashMachine(@RequestBody WashMachine washMachine){
        try {
            //添加洗衣机创建时间
            String Wash_machine_install_time = DateUtils.getCurrentTime();
            washMachine.setWash_machine_install_time(Wash_machine_install_time);
            //添加洗衣机信息

            //首先获取图片信息存入图片服务器
//            PicUploadResult fileUploadResult=picUploadService.upload(uploadFile,null);
            //返回的图片路径存入
//            washMachine.setWash_machine_picture(fileUploadResult.getName());
            washMachineService.addWashMachine(washMachine);
            //更新洗衣机数量
            List washingMachine = washHouseDao.getWashMachineInfo(washMachine.getWashhouse_id());
            washHouseDao.updWashingMachine(washMachine.getWashhouse_id(),washingMachine.size());
            //给新增洗衣机添加功能
            WashMachine newWashMachine = washMachineService.getNewWashingMachine(Wash_machine_install_time);
            long [] function = new long [] {1,2,3,4,5,6};
            for (int i = 0;i<function.length;i++){
                washMachineService.addWashMachineFunction(newWashMachine.getWash_machine_id(),function[i]);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        List<WashMachine> list = null;
        try {
            list = washMachineService.getWashMachineList(washMachine.getWashhouse_id());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new MassageJsons("0","添加成功",list);
    }
    /**
     * 删除洗衣机
     */
    @RequestMapping("/delWashMachineState")
    public MassageJsons delWashMachineState(@RequestBody HashMap<String,Long> hashMap){
        List<WashMachine> list = null;
        try {
            //修改洗衣机状态
            washMachineService.updWashMachineState(hashMap.get("wash_machine_id"),hashMap.get("wash_machine_state"));
            //查询所有洗衣机，返回洗衣机最新状态
            list = washMachineService.getWashMachineList(hashMap.get("washhouse_id"));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new MassageJsons("0","删除成功",list);
    }

    /**
     * 获取图片地址返回给前端
     */
    @RequestMapping("/tuxiangdizhi")
    public String tuxiangdizhi(@RequestBody MultipartFile file){
        System.out.println("file========================"+file);
        PicUploadResult picUploadResult= picUploadService.upload(file,null);
        System.out.println(picUploadResult.getName());
        return picUploadResult.getName();
    }
}
